/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package g5.fw;

import java.io.IOException;
import java.sql.Connection;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.HttpJspPage;

/**
 *
 * @author hue
 */
public class GenericServlet  extends HttpServlet implements HttpJspPage {
    
    @Override
    public final void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException {
        genericProcess(request, response);
    }
    
    @Override
    public final void doPost(final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException {
        genericProcess(request, response);
    }
    
    private void genericProcess(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        Throwable exception = null;
        try {
            ProcessType returnType = process(request, response);
            closeConnection(request);

            if(returnType == ProcessType.RUN_JSP) {
                try {
                    _jspService(request,response);
                }
                catch(ServletException ex) {
                    if(ex.getRootCause() != null) {
                        throw ex.getRootCause();
                    }
                    else {
                        throw ex;
                    }
                }
            }
        }
        catch(Throwable ex) {
            exception = ex;
        }
        finally {
            closeConnection(request);
            if(exception != null) {
                //showException(response, exception);
            }
        }
        try {
            response.flushBuffer();
        }
        catch(java.io.IOException ex) {
            //do nothing
        }
    }
    
    protected ProcessType process(final HttpServletRequest request, final HttpServletResponse response) throws Throwable {
        return ProcessType.RUN_JSP;
    }
    
    protected Connection getConnection(HttpServletRequest request) {
        Connection con = null;
        
        return con;
    }
    
    protected void closeConnection(HttpServletRequest request) {
    }
    
    @Override
    public final void init(final ServletConfig config) throws ServletException {
        super.init(config);
        jspInit();
        _jspInit();
    }

    @Override
    public void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    @Override
    public void jspInit() {
    }

    @Override
    public void jspDestroy() {
    }
    
    public void _jspInit() {}
    
    @Override
    public void destroy() {
        super.destroy();
        jspDestroy();
    }
}
